
package wsepForum.network.server;

import java.net.Socket;

import wsepForum.domain.ISessionObserver;
import wsepForum.domain.Session;
import wsepForum.network.common.GUIAnswer;
import wsepForum.network.common.GUIRequest;
import wsepForum.network.common.RequestSerializer;
import wsepForum.network.common.GUIAnswer.AnswerType;
import wsepForum.network.server.gate.IUIController;

/**
* Manage a single client connection, hold its session,
* and in charge of handling the GUI the right tokens/send data back to the client
*
* @author Oren Ben-Haim
*/
public class GUIConnectionHandler extends ConnectionHandler<GUIToken<GUIRequest>> implements ISessionObserver {


	/*****************************
	 * CONSTRUCTOR               *
	 *****************************/
	public GUIConnectionHandler(Socket acceptedSocket, Session connectionSession, IUIController<GUIToken<GUIRequest>> uiController) {
		super(acceptedSocket, connectionSession, uiController);
		this.tokenizer = new GUITokenizer<GUIRequest>();
		this.delimiter = "\0";
		this.session.addSessionObserver(this);
	}
	
	/*****************************
	 * MEMBERS                   *
	 *****************************/
	/* (non-Javadoc)
	 * @see wsepForum.domain.ISessionObserver#onDisconnect()
	 */
	@Override
	public void onLogoutDemand() {
		// generate disconnection packet
		String logoutAns = RequestSerializer.serialize(new GUIAnswer(AnswerType.ANSWER,new Object[]{true}, 0, this.session.getHandler().getNumOfUsers()));
		this.sendString(logoutAns);
		this.session.logout();
	}

	/* (non-Javadoc)
	 * @see wsepForum.domain.ISessionObserver#onConnect()
	 */
	@Override
	public void onLocalChange() {
		// no need for this type
	}
}
